package com.atguigu.eduservice.service.impl;

import com.atguigu.eduservice.entity.EduChapter;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.entity.chapter.ChapterVo;
import com.atguigu.eduservice.entity.chapter.VideoVo;
import com.atguigu.eduservice.mapper.EduChapterMapper;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-10-10
 */
@Service
public class EduChapterServiceImpl extends ServiceImpl<EduChapterMapper, EduChapter> implements EduChapterService {
    //课程大纲列表，根据课程id进行查询
    @Autowired
    private EduVideoService VideoService;
    @Override
    public List<ChapterVo> getChapterVideoByCourseId(String courseId) {
        //根据id查询所有章节
        QueryWrapper<EduChapter> wrapperChapter = new QueryWrapper<>();
        wrapperChapter.eq("course_id",courseId);
        List<EduChapter> eduChapterList = baseMapper.selectList(wrapperChapter);
        //根据id查询所有的小节
        QueryWrapper<EduVideo> wrapperVideo = new QueryWrapper<>();
       wrapperVideo.eq("course_id",courseId);
        List<EduVideo> eduVideoList = VideoService.list(wrapperVideo);
        //创建封装一切的集合
        ArrayList<ChapterVo> finalList = new ArrayList<>();
        //封装章节
        for (int i=0;i<eduChapterList.size();i++){
            EduChapter eduChapter = eduChapterList.get(i);
            ChapterVo chapterVo = new ChapterVo();
            BeanUtils.copyProperties(eduChapter,chapterVo);
            finalList.add(chapterVo);

            //得到对应章节的id
            String cid = eduChapter.getId();
            //创建包含查询list集合，进行封装
            ArrayList<VideoVo> videoList = new ArrayList<>();
            //封装小节

            for (int m=0;m<eduVideoList.size();m++){
               //得到每个小节
                EduVideo eduVideo = eduVideoList.get(m);
                //判断：小节里面chapterid和章节id是否一样
                if (eduVideo.getChapterId().equals(eduChapter.getId())){
                    VideoVo videoVo = new VideoVo();
                    BeanUtils.copyProperties(eduVideo,videoVo);
                    videoList.add(videoVo);
                   }
                }
            chapterVo.setChildren(videoList);
        }

        return finalList;
    }
    //删除章节
    @Override
    public boolean deleteChapter(String chapterId) {
        //根据章节id查询小节
        QueryWrapper<EduVideo> wrapper = new QueryWrapper<>();
        wrapper.eq("chapter_id",chapterId);
        int count = VideoService.count(wrapper);
        //判断是否有小节
        if(count>0){
            //如果有小节，那就不能删除
            throw new GuliException(20001,"不能删除！");
        }else {
            //如果没有小节，那就可以删除
            int result = baseMapper.deleteById(chapterId);
            //删除成功返回一
            //删除失败，返回0
            return result>0;
        }
    }
    //2.根据课程id删除章节
    @Override
    public void removeChapterByCourseId(String courseId) {
        QueryWrapper<EduChapter> wrapper = new QueryWrapper<>();
          wrapper.eq("course_id", courseId);
          baseMapper.delete(wrapper);
    }
}

